package com.hsy.baoinbao.modules.main.ui.find;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.deparse.dpframe.utils.DPGsonUtil;
import com.deparse.dpframe.utils.DPHttpUtil;
import com.deparse.dpframe.utils.DPLogUtil;
import com.hsy.baoinbao.R;
import com.hsy.baoinbao.common.Consts;
import com.hsy.baoinbao.modules.common.CommonBaseActivityWithSimpleActionBar;
import com.hsy.baoinbao.modules.main.ui.gsondata.FindData;
import com.hsy.baoinbao.utils.CommonUtils;
import com.hsy.baoinbao.utils.HttpUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/5/16.
 */
public class FindPhoneActivity extends CommonBaseActivityWithSimpleActionBar {
    private LayoutInflater inflater;
    private List<FindData.Data> listData;
    private ListView listView;
    private TextView btn_search;
    private EditText keywords;
    private FindAdapter findAdapter;
    private DPHttpUtil.HttpCallback httpCallBack = new DPHttpUtil.HttpCallback() {
        @Override
        public void onSuccess(int what, String response) {

            FindData.Result findData = DPGsonUtil.fromJson(response, FindData.Result.class);
            listData = new ArrayList<>();

            listData = findData.getData();
            findAdapter = new FindAdapter();
            listView.setAdapter(findAdapter);
            CommonUtils.hideProgressDialog();
        }

        @Override
        public void onError(int what, int type, VolleyError error) {
            CommonUtils.hideProgressDialog();

        }

    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_find_phone);

        inflater = getLayoutInflater();

        setTitle("搜索");

        listView = F(R.id.list_view);

        btn_search = F(R.id.btn_search);

        keywords = F(R.id.keywords);

        btn_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isEmpty(keywords.getText().toString())) {
                    showToast("关键字不能为空!");
                } else {
                    CommonUtils.showProgressDialog(FindPhoneActivity.this);
                    Map<String, String> data = new HashMap<String, String>();
                    data.put("name_merchant", keywords.getText().toString());
                    data.put("token", CommonUtils.getToken(FindPhoneActivity.this));
                    HttpUtil.httpPostWithParams(FindPhoneActivity.this, 0, Consts.API_POST_ORDER_SEARCH_VAGUE, data, httpCallBack);
                }

            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = getIntent();
                intent.putExtra("phone984988", listData.get(position).getUser_name());
                if (intent.getStringExtra("type").equals("receiving")) {
                    setResult(1917, intent);
                } else if (intent.getStringExtra("type").equals("goods")) {
                    setResult(1919, intent);
                }

                finish();

            }
        });

    }

    private class FindAdapter extends BaseAdapter {

        public FindAdapter() {

        }

        @Override
        public int getCount() {
            return listData.size();
        }

        @Override
        public Object getItem(int position) {
            return listData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            convertView = inflater.inflate(R.layout.item_find_list, null);

            TextView item_phone = (TextView) convertView.findViewById(R.id.item_phone);
            TextView item_name = (TextView) convertView.findViewById(R.id.item_name);

            item_name.setText(listData.get(position).getName_merchant());
            item_phone.setText(listData.get(position).getUser_name());
            return convertView;
        }
    }


}
